package com.om.abom.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.om.abom.entity.UpgradeRecordEntity;
import com.om.abom.service.UpgradeRecordService;
import com.om.abom.utils.PageResult;
import com.om.abom.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;


@RestController
@RequestMapping("upgradeRecord")
@CrossOrigin
@Slf4j
public class UpgradeRecordController
{
    @Autowired
    private UpgradeRecordService upgradeRecordService;

    /**
     * 分页
     */
    @GetMapping("/page")
    public R<PageResult<UpgradeRecordEntity>> page(@RequestParam(defaultValue = "1") int page,
                                               @RequestParam(defaultValue = "10") int pageSize,
                                               @RequestParam() int serverId)
    {
        Page<UpgradeRecordEntity> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<UpgradeRecordEntity> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(UpgradeRecordEntity::getBackupServer, serverId);

        queryWrapper.orderByDesc(UpgradeRecordEntity::getUpgradeTime);

        upgradeRecordService.page(pageInfo, queryWrapper);

        return R.success(new PageResult<>(pageInfo));
    }


    /**
     * 信息
     */
    @GetMapping("/{id}")
    public R<UpgradeRecordEntity> get(@PathVariable("id") Long id)
    {
        UpgradeRecordEntity upgradeRecord = upgradeRecordService.getById(id);

        return R.success(upgradeRecord);
    }

    /**
     * 修改
     */
    @PutMapping
    public R<Null> update(@RequestBody UpgradeRecordEntity upgradeRecord)
    {
        upgradeRecordService.updateById(upgradeRecord);

        return R.success();
    }

    /**
     * 删除
     */
    @DeleteMapping
    public R<Null> delete(@RequestBody Long[] ids)
    {
        upgradeRecordService.removeByIds(Arrays.asList(ids));

        return R.success();
    }
}

